package com.cskaoyan.service;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.goods.MarketGoods;
import com.cskaoyan.bean.user.Footprint;
import com.cskaoyan.bean.user.FootprintExample;
import com.cskaoyan.bean.wxfootprint.WxFootprintVo;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.WxFootprintMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsMapper;
import com.cskaoyan.util.DateUtils;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * \* Description:
 * \
 */
@Service
public class WxFootprintServiceImpl implements WxFootprintService {
    @Autowired
    WxFootprintMapper wxFootprintMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public BaseData<WxFootprintVo> list(Integer limit, Integer page) {
        PageHelper.startPage(page, limit);
        String primaryPrincipal = (String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        int id = wxFootprintMapper.getIdByPrimaryPrincipal(primaryPrincipal);


        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andIdEqualTo(id);
//获得goodsId  addTime  Id

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);


        ArrayList<WxFootprintVo> wxFootprintVos = new ArrayList<>();

        //从goods表查询数据  brief 商品简介  picurl 图片地址 retailprice 零售价格 name 商品名
        for (Footprint footprint : footprints) {
            Integer goodsId = footprint.getGoodsId();
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsId);
            WxFootprintVo wxFootprintVos1 = new WxFootprintVo();
            wxFootprintVos1.setBrief(marketGoods.getBrief());
            wxFootprintVos1.setPicUrl(marketGoods.getPicUrl());
            Date addTime = footprint.getAddTime();
            String date = DateUtils.dateFormat(addTime);
            wxFootprintVos1.setAddTime(date);
            wxFootprintVos1.setGoodsId(goodsId);
            wxFootprintVos1.setName(marketGoods.getName());
            wxFootprintVos1.setId(footprint.getId());
            wxFootprintVos1.setRetailPrice(marketGoods.getRetailPrice());

            wxFootprintVos.add(wxFootprintVos1);
        }
        PageInfo<WxFootprintVo> wxFootprintVoPageInfo = new PageInfo<>(wxFootprintVos);
        BaseData<WxFootprintVo> wxFootprintVoBaseData = new BaseData<>(wxFootprintVoPageInfo, wxFootprintVos);
        return wxFootprintVoBaseData;
    }

    @Override
    public void delete(Integer id) {
        wxFootprintMapper.delete(id);
    }
}
